/**
 * Created by yx on 2017/9/3.
 */
function reloadStatPage() {
    var diseaseStartDay = $("#diseaseStartDay").val();
    var diseaseEndDay = $("#diseaseEndDay").val();

    var warningStartDay = $("#warningStartDay").val();
    var warningEndDay = $("#warningEndDay").val();

    alert(diseaseStartDay);
    alert(diseaseEndDay);
    alert(warningStartDay);
    alert(warningEndDay);


}

function reloadDiseaseStatPage() {

    var diseaseStartDay = $("#diseaseStartDay").val();
    var diseaseEndDay = $("#diseaseEndDay").val();

    var a = /^(\d{4})-(\d{2})-(\d{2})$/;


    if(!a.test(diseaseStartDay) || !a.test(diseaseEndDay)){
        alert("日期格式有误，请输入yyyy-mm-dd格式");
        return false;
    }

    $.get("/stat/diseasestat?diseaseStartDay="+diseaseStartDay+'&diseaseEndDay='+diseaseEndDay,function(data,status) {
        var health_option = {
            title: {
                text: '',
                subtext: '',
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
                orient: 'vertical',
                x: 'left',
                data: ['7天以内', '30天以内', '30天以上']
            },
            toolbox: {
                show: true,
                feature: {
                    mark: {show: true},
                    dataView: {show: true, readOnly: false},
                    magicType: {
                        show: true,
                        type: ['pie', 'funnel'],
                        option: {
                            funnel: {
                                x: '25%',
                                width: '50%',
                                funnelAlign: 'left',
                                max: 1548
                            }
                        }
                    },
                    restore: {show: true},
                    saveAsImage: {show: true}
                }
            },
            calculable: true,
            series: [
                {
                    name: '访问来源',
                    type: 'pie',
                    radius: '55%',
                    center: ['50%', '60%'],
                    data: [
                        {value: data.diseaseStat.lt7Cnt, name: '7天以内'},
                        {value: data.diseaseStat.lt30Cnt, name: '30天以内'},
                        {value: data.diseaseStat.gt30Cnt, name: '30天以上'}
                    ]
                }
            ]
        };

        require(
            [
                'echarts',
                'echarts/chart/pie' // 使用柱状图就加载bar模块，按需加载
            ],
            function (ec) {
                var healthChart = ec.init(document.getElementById("health_stat"));
                healthChart.setOption(health_option);

            }
        );
    });

}

function reloadWarningPage() {
    var warningStartDay = $("#warningStartDay").val();
    var warningEndDay = $("#warningEndDay").val();
    var a = /^(\d{4})-(\d{2})-(\d{2})$/;


    if(!a.test(warningStartDay) || !a.test(warningEndDay)){
        alert("日期格式有误，请输入yyyy-mm-dd格式");
        return false;
    }

    $.get("/stat/warningstat?warningStartDay="+warningStartDay+'&warningEndDay='+warningEndDay,function(data,status) {

        
        var xString = [];
        var warningCntData = [];
        var totalCntData = [];
        var maxWarningCnt = 0;
        var maxWarningIndex = 0;
        var minWarningCnt = 10000;
        var minWarningIndex = 0;
        
        
        for(var gs in data.warningStat){
            xString.push(data.warningStat[gs].logday);
            warningCntData.push(data.warningStat[gs].waringCnt);
            totalCntData.push(data.warningStat[gs].totalCnt);
            
            if(maxWarningCnt<data.warningStat[gs].waringCnt){
                maxWarningCnt = data.warningStat[gs].waringCnt;
                maxWarningIndex=gs;
            }
            if(minWarningCnt>data.warningStat[gs].waringCnt){
                minWarningCnt = data.warningStat[gs].waringCnt;
                minWarningIndex=gs;
            }
            
        }
        
        
        var warning_option = {
            title : {
                text: '',
                subtext: ''
            },
            tooltip : {
                trigger: 'axis'
            },
            legend: {
                data:['预警次数','总人数']
            },
            toolbox: {
                show : true,
                feature : {
                    mark : {show: true},
                    dataView : {show: true, readOnly: false},
                    magicType : {show: true, type: ['line', 'bar']},
                    restore : {show: true},
                    saveAsImage : {show: true}
                }
            },
            calculable : true,
            xAxis : [
                {
                    type : 'category',
                    data : xString
                }
            ],
            yAxis : [
                {
                    type : 'value'
                }
            ],
            series : [
                {
                    name:'预警次数',
                    type:'bar',
                    data:warningCntData,
                    markPoint : {
                        data : [
                            {type : 'max', name: '最大值'},
                            {type : 'min', name: '最小值'}
                        ]
                    },
                    markLine : {
                        data : [
                            {type : 'average', name: '平均值'}
                        ]
                    }
                },
                {
                    name:'总人数',
                    type:'bar',
                    data:totalCntData,
                    markPoint : {
                        data : [
                            {type : 'max', name: '最大值'},
                            {type : 'min', name: '最小值'}
                        ]
                    },
                    markLine : {
                        data : [
                            {type : 'average', name : '平均值'}
                        ]
                    }
                }
            ]
        };



        require.config({
            paths: {
                echarts: '/js/echarts/dist'
            }
        });

        require(
            [
                'echarts',
                'echarts/chart/bar' // 使用柱状图就加载bar模块，按需加载
            ],
            function (ec) {
                // 基于准备好的dom，初始化echarts图表
                var chart1 = ec.init(document.getElementById("chart2"));

                // 为echarts对象加载数据
                chart1.setOption(warning_option);
            }
        );
        
        
    });
}